package org.icec.iot.mqtt.server.plugin.auth;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@PropertySource(value = { "classpath:mqtt-db.properties" })
public class DbConfig {
	@Value(value = "${datasource.driver}")
	private String driverClassName;
	@Value(value = "${datasource.url}")
	private String url;
	@Value(value = "${datasource.username}")
	private String username;
	@Value(value = "${datasource.password}")
	private String password;

	@Bean(name = "datasource")
	public DataSource druidDataSource(Environment env) {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setDriverClassName(driverClassName);
		druidDataSource.setUrl(url);
		druidDataSource.setUsername(username);
		druidDataSource.setPassword(password);
		druidDataSource.setValidationQuery("SELECT 1 ");
		druidDataSource.setInitialSize(5);
		druidDataSource.setMaxActive(10);
		return druidDataSource;
	}
}
